package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.controller.admin.employee.train.EmployeeTrainingExperienceSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeTrainingExperienceDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeTrainingExperienceMapper;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 员工培训经历 Service 实现类
 *
 * @author tengcj
 */
@Service
@Validated
public class EmployeeTrainingExperienceServiceImpl implements EmployeeTrainingExperienceService {

    @Resource
    private EmployeeTrainingExperienceMapper employeeTrainingExperienceMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public Long createEmployeeTrainingExperience(EmployeeTrainingExperienceSaveReqVO createReqVO) {
        // 插入
        EmployeeTrainingExperienceDO employeeTrainingExperience = BeanUtils.toBean(createReqVO, EmployeeTrainingExperienceDO.class);
        // 校验时间有效性
        validateTrainingExperienceTime(createReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(createReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        List<EmployeeTrainingExperienceDO> experienceList = getEmployeeTrainingExperienceList(createReqVO.getEmployeeId());
        employeeTrainingExperience.setSort(CollUtil.isEmpty(experienceList) ? 0 : experienceList.get(0).getSort() + 1);
        employeeTrainingExperienceMapper.insert(employeeTrainingExperience);
        // 返回
        return employeeTrainingExperience.getId();
    }

    @Override
    public void updateEmployeeTrainingExperience(EmployeeTrainingExperienceSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeTrainingExperienceExists(updateReqVO.getId());
        // 校验时间有效性
        validateTrainingExperienceTime(updateReqVO);

        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(updateReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        // 更新
        EmployeeTrainingExperienceDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeTrainingExperienceDO.class);
        employeeTrainingExperienceMapper.updateById(updateObj);
    }

    public void validateTrainingExperienceTime(EmployeeTrainingExperienceSaveReqVO vo) {
        LocalDateTime startTime = vo.getStartTime();
        LocalDateTime endTime = vo.getEndTime();
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime)) {
            if (startTime.compareTo(endTime) > 0) {
                throw exception(TRAINING_START_TIME_UNLATER_END_TIME);
            }
        }
    }


    @Override
    public void deleteEmployeeTrainingExperience(Long id) {
        // 校验存在
        validateEmployeeTrainingExperienceExists(id);
        // 删除
        employeeTrainingExperienceMapper.deleteById(id);
    }

    private void validateEmployeeTrainingExperienceExists(Long id) {
        if (employeeTrainingExperienceMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_TRAINING_EXPERIENCE_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeTrainingExperienceDO getEmployeeTrainingExperience(Long id) {
        return employeeTrainingExperienceMapper.selectById(id);
    }

    @Override
    public List<EmployeeTrainingExperienceDO> getEmployeeTrainingExperienceList(Long employeeId) {
        return employeeTrainingExperienceMapper.getEmployeeTrainingExperienceList(employeeId);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        employeeTrainingExperienceMapper.delete(new LambdaQueryWrapper<EmployeeTrainingExperienceDO>(){{
            eq(EmployeeTrainingExperienceDO::getEmployeeId, employeeId);
        }});
    }
}
